import 'package:flutter/material.dart';
import 'package:jdflutter/flutter_jdshop/pages/tabs/Cart.dart';
import 'package:jdflutter/flutter_jdshop/pages/tabs/Category.dart';
import 'package:jdflutter/flutter_jdshop/pages/tabs/Home.dart';
import 'package:jdflutter/flutter_jdshop/pages/tabs/User.dart';
import 'package:jdflutter/flutter_jdshop/services/ScreenAdaper.dart';

class Tabs extends StatefulWidget {
  const Tabs({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {

  int _currentIndex = 0;
  // 创建页面控制器
  late var _pageController;
  List<Widget> _pageList=[
    //Home
    HomePage(),
    CategoryPage(),
    CartPage(),
    UserPage(),
  ];
  @override
  void initState() {
    // TODO: implement initState
    // 页面控制器初始化
    _pageController = new PageController(initialPage: _currentIndex);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _currentIndex!=3?AppBar(
        leading: IconButton(
          icon: Icon(Icons.center_focus_weak,size: 28,color: Colors.black87),
          onPressed: null,
        ),
        title: InkWell(
          child: Container(
            height: ScreenAdaper.height(68),
            decoration: BoxDecoration(
              color: Color.fromRGBO(233, 233, 233, 0.8),
              borderRadius: BorderRadius.circular(30)
            ),
            padding: EdgeInsets.only(left: 10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Icon(Icons.search),
                Text("笔记本",style: TextStyle(fontSize: ScreenAdaper.size(28)),)
              ],
            ),
          ),
          onTap: (){
            Navigator.pushNamed(context, "/search");
          },
        ),
        actions: <Widget>[
          IconButton(onPressed: null, icon: Icon(Icons.message,size: 28,color: Colors.black87))
        ],
      ):AppBar(
        title: Text("用户中心"),
      ),
      // IndexedStack
      // body:IndexedStack(
      //   index: this._currentIndex,
      //   children: _pageList,
      // ),

      // 必须用PageView加载不同的页面
      body: PageView(
        controller: _pageController,
        children: this._pageList,
        onPageChanged: (index){
          _currentIndex=index;
        },
      ),
      // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: this._currentIndex,
        onTap: (index){
          setState(() {
            this._currentIndex= index;
            // 页面控制器进行跳转
            _pageController.jumpToPage(this._currentIndex);
          });
        },
        type: BottomNavigationBarType.fixed,
        fixedColor: Colors.red,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "首页"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.category),
              label: "分类"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_cart),
              label: "购物车"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.people),
              label: "我的"
          ),
        ],
      ),
    );
  }
}
